package com.frxuexi.visit.view

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.VisitBeanItem
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.utils.GlideUtils
import com.frxuexi.visit.R
import com.frxuexi.visit.VisitIntent
import com.frxuexi.visit.VisitViewModel
import com.frxuexi.visit.databinding.ActivityVisitAddBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/visit/VisitAddActivity")
@AndroidEntryPoint
class VisitAddActivity : BaseActivity<ActivityVisitAddBinding,VisitIntent,VisitViewModel>() {
    override val binding: ActivityVisitAddBinding by lazy { ActivityVisitAddBinding.inflate(layoutInflater) }
    override val vm: VisitViewModel by viewModels<VisitViewModel>()
    val imgs= mutableListOf<String>()

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DEFAULT->{
                                ToastUtils.showLong("添加成功")
                                finish()
                            }
                            RequestType.UPLOAD->{
                                val s = it.data as String
                                val name = s.split(":")[1]
                                Log.i("lr","http://10.161.9.80:2209/images/visit/${SPUtils.getInstance().getInt(Const.UID)}/${name}")
                                imgs.add( "http://10.161.9.80:2209/images/visit/${SPUtils.getInstance().getInt(Const.UID)}/${name}")
                            }

                            else -> {}
                        }

                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.visitaddIvDate.setOnClickListener {
            showDateDialog {
                binding.visitaddDate.setText(it)
            }
        }

        binding.visitaddIvStart.setOnClickListener {
            showTimeDialog {
                binding.visitaddStart.setText(it)
            }
        }
        binding.visitaddIvEnd.setOnClickListener {
            showTimeDialog {
                binding.visitaddEnd.setText(it)
            }
        }

        binding.visitaddSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
            binding.visitaddCar.isEnabled=isChecked
        }

        binding.visitaddName.setText(SPUtils.getInstance().getString(Const.UNAME))

        binding.visitaddSave.setOnClickListener {
            val date = binding.visitaddDate.text.toString()
            val start = binding.visitaddStart.text.toString()
            val people = binding.visitaddPeople.text.toString()
            val cardId = binding.visitaddCardId.text.toString()
            val phone = binding.visitaddPhone.text.toString()
            val carNo = binding.visitaddCar.text.toString()
            val purpose = binding.visitaddPurpose.text.toString()
            val name = binding.visitaddName.text.toString()

            val item = VisitBeanItem(
                "${date} ${start}", false, imgs, people.toInt(), null, cardId,
                phone, carNo, purpose, binding.visitaddSwitch.isChecked, "待审核", name,0
            )
            vm.sendIntent(VisitIntent.addVisit(item))
        }

        binding.visitaddIv.setOnClickListener {
            PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==PictureSelector.SELECT_REQUEST_CODE && resultCode== RESULT_OK){
            val bean = data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            GlideUtils.loadImage(bean?.path!!,binding.visitaddIv)
            val path = bean.path
            val split = path.split("/")
            val name = split.get(split.lastIndex)
            vm.sendIntent(VisitIntent.upload(name,path))
        }
    }
}